In [40]:
from sympy import *
from sympy.utilities.iterables import subsets
init_printing()
from IPython.display import *
In [41]:
s,t,x,u,v=symbols('s t x u v')
var('alpha')
Out[41]:
$$\alpha$$
In [42]:
def perm(A):
    if A.shape==(1,1):
        return A[0,0]
    else:
        n=A.shape[0]
        return sum([A[0,i]*perm(A.minorMatrix(0,i)) for i in range(n)])
In [43]:
def derange(n):
    if n==0:
        return 1
    else:
        return n*derange(n-1)+(-1)**n
In [44]:
for d in range(1,6):
    A=t*ones(d,d)+s*eye(d)    
    display(expand(simplify(perm(A))))
$$s + t$$
$$s^{2} + 2 s t + 2 t^{2}$$
$$s^{3} + 3 s^{2} t + 6 s t^{2} + 6 t^{3}$$
$$s^{4} + 4 s^{3} t + 12 s^{2} t^{2} + 24 s t^{3} + 24 t^{4}$$
$$s^{5} + 5 s^{4} t + 20 s^{3} t^{2} + 60 s^{2} t^{3} + 120 s t^{4} + 120 t^{5}$$
In [45]:
[perm(ones(d,d)-eye(d)) for d in range(1,9)]
Out[45]:
$$\left [ 0, \quad 1, \quad 2, \quad 9, \quad 44, \quad 265, \quad 1854, \quad 14833\right ]$$
In [46]:
y=[derange(i) for i in range(21)]
y
Out[46]:
$$\left [ 1, \quad 0, \quad 1, \quad 2, \quad 9, \quad 44, \quad 265, \quad 1854, \quad 14833, \quad 133496, \quad 1334961, \quad 14684570, \quad 176214841, \quad 2290792932, \quad 32071101049, \quad 481066515734, \quad 7697064251745, \quad 130850092279664, \quad 2355301661033953, \quad 44750731559645106, \quad 895014631192902121\right ]$$
In [47]:
def arrange(n):
    if n==0:
        return 1
    else:
        return n*arrange(n-1)+1
In [48]:
[perm(ones(d,d)+eye(d)) for d in range(1,9)]
Out[48]:
$$\left [ 2, \quad 5, \quad 16, \quad 65, \quad 326, \quad 1957, \quad 13700, \quad 109601\right ]$$
In [49]:
y=[arrange(i) for i in range(21)]
y
Out[49]:
$$\left [ 1, \quad 2, \quad 5, \quad 16, \quad 65, \quad 326, \quad 1957, \quad 13700, \quad 109601, \quad 986410, \quad 9864101, \quad 108505112, \quad 1302061345, \quad 16926797486, \quad 236975164805, \quad 3554627472076, \quad 56874039553217, \quad 966858672404690, \quad 17403456103284421, \quad 330665665962404000, \quad 6613313319248080001\right ]$$
In [50]:
def h(a,b,s,t):
    return integrate(exp(-x)*(s+t*x)**a*(t*x)**b,(x,0,oo))
In [51]:
# This gives arrangements
[h(k,0,1,1) for k in range(21)]
Out[51]:
$$\left [ 1, \quad 2, \quad 5, \quad 16, \quad 65, \quad 326, \quad 1957, \quad 13700, \quad 109601, \quad 986410, \quad 9864101, \quad 108505112, \quad 1302061345, \quad 16926797486, \quad 236975164805, \quad 3554627472076, \quad 56874039553217, \quad 966858672404690, \quad 17403456103284421, \quad 330665665962404000, \quad 6613313319248080001\right ]$$
In [52]:
# This give derangements
[h(k,0,-1,1) for k in range(21)]
Out[52]:
$$\left [ 1, \quad 0, \quad 1, \quad 2, \quad 9, \quad 44, \quad 265, \quad 1854, \quad 14833, \quad 133496, \quad 1334961, \quad 14684570, \quad 176214841, \quad 2290792932, \quad 32071101049, \quad 481066515734, \quad 7697064251745, \quad 130850092279664, \quad 2355301661033953, \quad 44750731559645106, \quad 895014631192902121\right ]$$
In [53]:
k=2
Z=list(subsets(range(d),k))
Z
Out[53]:
$$\left [ \left ( 0, \quad 1\right ), \quad \left ( 0, \quad 2\right ), \quad \left ( 0, \quad 3\right ), \quad \left ( 0, \quad 4\right ), \quad \left ( 1, \quad 2\right ), \quad \left ( 1, \quad 3\right ), \quad \left ( 1, \quad 4\right ), \quad \left ( 2, \quad 3\right ), \quad \left ( 2, \quad 4\right ), \quad \left ( 3, \quad 4\right )\right ]$$
In [54]:
def zpow(E,k):
    d=E.shape[0]
    N=binomial(d,k)
    Z=list(subsets(range(d),k))
    return Matrix(N,N,lambda i,j: expand(perm(E.extract(Z[i],Z[j]))))
In [55]:
d=4
A=t*ones(d,d)+s*eye(d) 
A
Out[55]:
$$\left[\begin{matrix}s + t & t & t & t\\t & s + t & t & t\\t & t & s + t & t\\t & t & t & s + t\end{matrix}\right]$$
In [56]:
l=2
Al=zpow(A,l)
Al
Out[56]:
$$\left[\begin{matrix}s^{2} + 2 s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & 2 t^{2}\\s t + 2 t^{2} & s^{2} + 2 s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & 2 t^{2} & s t + 2 t^{2}\\s t + 2 t^{2} & s t + 2 t^{2} & s^{2} + 2 s t + 2 t^{2} & 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2}\\s t + 2 t^{2} & s t + 2 t^{2} & 2 t^{2} & s^{2} + 2 s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2}\\s t + 2 t^{2} & 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & s^{2} + 2 s t + 2 t^{2} & s t + 2 t^{2}\\2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & s t + 2 t^{2} & s^{2} + 2 s t + 2 t^{2}\end{matrix}\right]$$
In [57]:
n=A.shape[0]
display([h(l-i,i,s,t) for i in range(1+min(l,n-l))])
display([expand(simplify(s**alpha*t**(alpha+l-n)/factorial(n-l-alpha)*h(l-alpha,n-l-alpha,s,t))) for alpha in range(1+min(l,n-l))])
[binomial(n,alpha)-binomial(n,alpha-1) for alpha in range(1+min(l,n-l))]
$$\left [ s^{2} + 2 s t + 2 t^{2}, \quad s t + 2 t^{2}, \quad 2 t^{2}\right ]$$
$$\left [ s^{2} + 6 s t + 12 t^{2}, \quad s^{2} + 2 s t, \quad s^{2}\right ]$$
Out[57]:
$$\left [ 1, \quad 3, \quad 2\right ]$$
In [58]:
Al.eigenvals()
Out[58]:
$$\left \{ s^{2} : 2, \quad s^{2} + 2 s t : 3, \quad s^{2} + 6 s t + 12 t^{2} : 1\right \}$$
$$\frac{s^\alpha t^{\alpha+l-n}}{(n-l-\alpha)!}\,h(l-\alpha,n-l-\alpha,s,t)$$
In [ ]:
 
In [ ]: